package com.isoftstone.framework;


import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.core.TemplateConfiguration;
import freemarker.template.TemplateException;
import freemarker.template.utility.XmlEscape;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.*;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;


import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration // 使用Configuration注解，自动构造一些内部定义的bean
@ConditionalOnClass({ freemarker.template.Configuration.class,
        FreeMarkerConfigurationFactory.class }) // 需要freemarker.template.Configuration和FreeMarkerConfigurationFactory这两个类存在在classpath中才会进行自动配置
@AutoConfigureAfter(WebMvcAutoConfiguration.class) // 本次自动配置需要依赖WebMvcAutoConfiguration这个配置类配置之后触发。这个WebMvcAutoConfiguration内部会配置很多Wen基础性的东西，比如RequestMappingHandlerMapping、RequestMappingHandlerAdapter等
public class FreeMarkerAutoConfiguration{

    @Bean
    public FreeMarkerViewResolver viewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(false);
        resolver.setRequestContextAttribute("request");
        resolver.setExposeSpringMacroHelpers(true);
        resolver.setExposeRequestAttributes(true);
        resolver.setExposeSessionAttributes(true);
        resolver.setSuffix(".html");
        resolver.setContentType("text/html; charset=UTF-8");
        return resolver;
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig()  throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:/templates/", "src/main/resources/templates/");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();

        freemarker.template.Configuration configuration = factory.createConfiguration();
        configuration.setClassicCompatible(true);

        configuration.setSharedVariable("getDict",new GetDict());

        Map<String, Object> map = new HashMap<>();
        map.put("c","/common/dictselect.html");
        configuration.setAutoImports(map);

        result.setConfiguration(configuration);

        result.afterPropertiesSet();
        result.getConfiguration().setSharedVariable("shiro",new ShiroTags());
        //FreeMarkerConfigurer
        TemplateConfiguration templateConfiguration = new TemplateConfiguration();

        templateConfiguration.setRecognizeStandardFileExtensions(true);

        Properties settings = new Properties();
        settings.put("template_update_delay", "0");
        settings.put("default_encoding", "UTF-8");
        settings.put("number_format", "0.##########");
        settings.put("datetime_format", "yyyy-MM-dd HH:mm:ss");
        settings.put("classic_compatible", true);
        settings.put("template_exception_handler", "ignore");
        settings.put("auto_import", "/common/dictselect.html as c");
        Map<String, Object> map1 = new HashMap<>();
        map.put("xml_escape", new XmlEscape());
        map.put("getDict",new GetDict());
        result.setFreemarkerVariables(map1);
        result.setFreemarkerSettings(settings);
        return result;
    }
}
